package com.personal.gateway.flinkAllCommand.configurationOption;

import com.personal.gateway.custom.customException.ConfigurationOptionCheckException;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationGenerator.AbstractConfigurationGenerator;
import com.personal.gateway.flinkAllCommand.configurationOption.configurationType.AbstractConfigurationType;
import com.personal.gateway.flinkAllCommand.configurationOption.parameterCheck.AbstractParameterCheck;
import com.personal.gateway.flinkAllCommand.configurationOption.webObjectGenerator.AbstractWebObjectGenerator;
import lombok.Getter;

/**
 * @author BigHead
 * @date 2022/1/10 14:57
 * @description ConfigurationOption TODO 1.多条件筛选，2.Enums的多选，3.输入框单位
 */
@Getter
public class ConfigurationOption<BT, CT extends AbstractConfigurationType<BT>> {
    /**
     * 组别
     */
    private final KeyIndexInfomationDao groupKey;
    /**
     * 配置文件key
     */
    private final KeyIndexInfomationDao configurationKey;
    /**
     * 页面配置展示名称
     */
    private final String webShowName;
    /**
     * 字段描述
     */
    private final String describe;
    /**
     * 是否必传
     */
    private final boolean necessary;
    /**
     * 参数类型
     */
    private final CT configurationType;
    /**
     * 页面代码生成工具类
     */
    private final Class<? extends AbstractWebObjectGenerator<BT, CT>> webObjectGenerator;
    /**
     * 参数值校验生成工具类
     */
    private final Class<? extends AbstractParameterCheck<BT, CT>> parameterCheck;
    /**
     * 配置文件生成工具类
     */
    private final Class<? extends AbstractConfigurationGenerator<BT, CT>> configurationGenerator;
    /**
     * 前提条件
     */
    private final ConfigurationOption<?, ?> antecedentConfigurationOption;
    /**
     * 校验异常
     */
    private ConfigurationOptionCheckException configurationOptionCheckException;
    /**
     * 是否存在带组装参数
     */
    private final boolean hasChildConfigurations;

    ConfigurationOption(
            KeyIndexInfomationDao groupKey,
            KeyIndexInfomationDao configurationKey,
            String webShowName,
            String describe,
            boolean necessary,
            CT configurationType,
            Class<? extends AbstractWebObjectGenerator<BT, CT>> webObjectGenerator,
            Class<? extends AbstractParameterCheck<BT, CT>> parameterCheck,
            Class<? extends AbstractConfigurationGenerator<BT, CT>> configurationGenerator,
            ConfigurationOption<?, ?> antecedentConfigurationOption,
            boolean hasChildConfigurations
    ) {
        this.groupKey = groupKey;
        this.configurationKey = configurationKey;
        this.webShowName = webShowName;
        this.describe = describe;
        this.necessary = necessary;
        this.configurationType = configurationType;
        this.webObjectGenerator = webObjectGenerator;
        this.parameterCheck = parameterCheck;
        this.configurationGenerator = configurationGenerator;
        this.antecedentConfigurationOption = antecedentConfigurationOption;
        this.hasChildConfigurations = hasChildConfigurations;
    }

    public String getWebElement(boolean isFirst) {
        // TODO 这里理论应该不会出现异常
        try {
            return webObjectGenerator.newInstance().getWebElement(this, isFirst);
        } catch (Exception e) {
            return "";
        }
    }

    public void setValue(String value) {
        // TODO 这里理论应该不会出现异常
        try {
            AbstractParameterCheck<BT, CT> abstractParameterCheck = parameterCheck.newInstance();
            configurationType.setValue(
                    abstractParameterCheck.checkValue(this, value)
            );
        } catch (ConfigurationOptionCheckException ce) {
            this.configurationOptionCheckException = ce;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getConfigurationString() {
        // TODO 这里理论应该不会出现异常
        try {
            AbstractConfigurationGenerator<BT, CT> abstractConfigurationGenerator = configurationGenerator.newInstance();
            return abstractConfigurationGenerator.configurationString(this);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
